﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using Yoozoo.Gameplay.City;

namespace Yoozoo.Gameplay.CityEditor
{
    [CustomEditor(typeof(RoomEventData))]
    public class RoomEventDataEditor : UnityEditor.Editor
    {
        private const string configPath = "Assets/ResourcesAssets/City/Config/cityEventConfig.json";

        private static CityEventConfig _eventConfig;
        private RoomEventData _roomEventData;

        private SerializedProperty m_idProp;
        
        private static GUIContent eventInreaderText =
            EditorGUIUtility.TrTextContent("事件集合", "用于小人生成、目标点以及事件执行");

        private SerializedProperty m_eventInListProp;
        private ReorderableList m_eventInRenderList;
        

        private void OnEnable()
        {
            _roomEventData = target as RoomEventData;
            //m_idProp = serializedObject.FindProperty("rID");

            //m_eventInListProp = serializedObject.FindProperty("RoomEventList");
            //m_eventInRenderList = new ReorderableList(serializedObject, m_eventInListProp, true, true, true, true);
            //DrawRoomEvent2InListLayout(m_eventInRenderList, m_eventInListProp);
        }

        public static void UpdateCityConfig()
        {
            // var jsonText = AssetDatabase.LoadAssetAtPath<TextAsset>(configPath);
            // _featureConfig = JsonUtility.FromJson<CityFeatureConfig>(jsonText.text);
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            serializedObject.Update();

            EditorGUILayout.Space(10);
            //m_eventInRenderList.DoLayoutList();
            EditorGUILayout.Space(10);
            serializedObject.ApplyModifiedProperties();
        }

        private void DrawRoomEvent2InListLayout(ReorderableList list, SerializedProperty prop)
        {
            //EditorGUIUtility.labelWidth = 100;
            list.elementHeight = EditorGUIUtility.singleLineHeight * 1.25f * 4 + 10;
            list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                var element = prop.GetArrayElementAtIndex(index);

                SerializedProperty idProperty = element.FindPropertyRelative("eventId");
                Rect idRect = new Rect(rect.x, rect.y, 280, EditorGUIUtility.singleLineHeight);
                idProperty.intValue = EditorGUI.IntField(idRect, "事件id", idProperty.intValue);

                rect.y += idRect.height + 5;

                //rect.y += posRect.height + 5;
                
                SerializedProperty stateProperty = element.FindPropertyRelative("state");
                Rect stateRect = new Rect(rect.x, rect.y, 280, EditorGUIUtility.singleLineHeight);
                EditorGUI.ObjectField(stateRect, stateProperty, new GUIContent("状态"));
            };
            list.drawHeaderCallback = (Rect rect) =>
            {
                EditorGUI.LabelField(rect, eventInreaderText);
            };

            list.onCanRemoveCallback = li => { return li.count > 0; };
        }
        private void DraweventPointListLayout(ReorderableList list, SerializedProperty prop)
        {
            list.elementHeight = EditorGUIUtility.singleLineHeight * 1.25f * 6 + 10;
            
            list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                EditorGUIUtility.labelWidth = 100;

                var element = prop.GetArrayElementAtIndex(index);
                rect.y += 2;
                rect.x += 10;
                
                SerializedProperty idProperty = element.FindPropertyRelative("hID");
                Rect idRect = new Rect(rect.x, rect.y, 360, EditorGUIUtility.singleLineHeight);
                idProperty.intValue = (int)(CityHudEnum)EditorGUI.EnumPopup(idRect, "", (CityHudEnum)idProperty.intValue);
                //idProperty.intValue = EditorGUI.IntField(idRect, "Hud类型ID", idProperty.intValue);
                
                rect.y += idRect.height + 5;
                SerializedProperty posProperty = element.FindPropertyRelative("hPos");
                Rect posRect = new Rect(rect.x, rect.y, 280, EditorGUIUtility.singleLineHeight);
                EditorGUI.ObjectField(posRect,posProperty,new GUIContent("挂点"));

            };
            
            list.drawHeaderCallback = (Rect rect) =>
            {
                EditorGUI.LabelField(rect, "事件挂点");
            };
            
            list.onCanRemoveCallback = li => { return li.count > 0; };   
        }
        
       
    }
}

